/*
 *  GraphicsLeakTest.cpp
 *  
 *
 *  Created by Daniel Condon on 28/09/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <OpenGL/OpenGL.h>

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

const int VIEW_DISTANCE = 500;
const int WINDOW_SIZE_X = 600;
const int WINDOW_SIZE_Y = 600;

void Init(){
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_CULL_FACE);
	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void display(){
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, VIEW_DISTANCE, 0, VIEW_DISTANCE);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT);
	
	glColor4f(0.0, 0.9, 0.3, 1.0);
	
	glBegin(GL_POINTS);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(1.0f, 0.0f);
	glVertex2f(-1.0f, 0.0f);
	glVertex2f(-1.0f, 1.0f);
	glVertex2f(1.0f, 1.0f);
	glEnd();
	
	glFlush();
}

void reshape(int w, int h){
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-VIEW_DISTANCE, VIEW_DISTANCE, -VIEW_DISTANCE, VIEW_DISTANCE);
	glutPostRedisplay();
}

void KeyboardController(GLubyte key, GLint x, GLint y){
	switch ((char)key) {
		case 27:
			exit(1);
			break;
		default:
			break;
	}
}

void mouseInput(int button, int state, int x, int y){
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		
	}
}

void mouseMove(int x, int y){
	//convertMouseCoord(x, y);
}

void LogicLoop(int value){
	glutPostRedisplay();
	glutTimerFunc(value, LogicLoop, value);
}

int main(int argc, char** argv){
	glutInitWindowSize(WINDOW_SIZE_X, WINDOW_SIZE_Y);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutCreateWindow("Graphics Leaker");
	
	Init();
	glutKeyboardFunc(KeyboardController);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouseInput);
	glutPassiveMotionFunc(mouseMove);
	
	LogicLoop(1000);
	glutMainLoop();
	return 0;
}